<%@page import="java.text.DecimalFormat"%>
<%@page import="org.proddevplm.dao.data.designInputs.CTQs"%>
<%@page import="org.proddevplm.dao.data.designInputs.ProductFunctions"%>
<%@page import="java.util.List"%>
<%@page import="org.proddevplm.constants.ControllerConstants"%>
<%@page import="org.proddevplm.business.activity.constants.ActivityConstants" %>

<script>
	qfdType = "ctqsVsFunctions";
	
	$(function(){
		$("button").button();
		$("#qfdResults").hide();
	})
	
	function autoFill(id){
		$('#'+id).autocomplete("search", " ");
	}
	
	$(function() {
		var qfdScale = [
			"++ ",
			"+ ",
			"O ",
			"- ",
		];
		$( ".qfdField" ).autocomplete({
			source: qfdScale,
		});
	});
	
	function calculateQFD(rows, cols){
		var matrixValues = '';
		for(var i=0; i<rows; i++){
			for(var j=0; j<cols; j++){
				var id = "elem_"+(i+1)+"_"+(j+1);
				var value = document.getElementById(id).value;
				if(value == "++ "){
					value = 9;
				}else if(value == "+ "){
					value = 3;
				}else if(value == "O "){
					value = 1;
				}else if(value == "- " || value== "" ){
					value = 0;
				}
				matrixValues+=value+"~";
			}
		}
		matrixValues = matrixValues.substring(0, matrixValues.length-1);
		
		var activityName = document.getElementById("activitySelect").value;
		
		var arguments = "activityName="+activityName+"&qfdMatrixValues="+matrixValues+"&qfdType="+qfdType;
		ajaxCall("toolsManager", "26", arguments, showQFDResults);
	}
	
	function showQFDResults(data){
		$("#qfdResults").show();
		var results = data.split("~");
		for(var k=0; k<results.length; k++){
			var val = parseFloat(results[k]);
			var shownVal = Math.round(val*100)/100;
			$("#imp_"+k).html(shownVal+"%");
		}
	}
	
	function finishQfd(){
		var activityName = document.getElementById("activitySelect").value;
		$("#activityName").val(activityName);
		document.forms["finishActivityForm"].submit();
	}

</script>


<%
	List<ProductFunctions> functions = (List)session.getAttribute("functions");
	List<CTQs> ctqs = (List)session.getAttribute("ctqs");
%>

<div align="center" style="margin-top:10px;">
	<table id="qfdTable" border="1" cellspacing="0" cellpadding="3" style="width:80%; font-size:small; ">
		
		<tr id="headerUp" style="height:80px; background-color:#F0F0F6">
			<td style="font-weight:bold; font-weight:bold; color:#555555">Functions</td>
			<%for(int i=0; i<functions.size(); i++){
				ProductFunctions pf = (ProductFunctions)functions.get(i);%>
				<td rowspan="2" style="padding:0"><span class="rotatedText" style="margin-bottom:0px; margin:-20px">
					<%=pf.getFunction() %>
					</span>
				</td>
			<%}%>
			<td rowspan="2" style="font-weight:bold; font-weight:bold; color:#555555">Importance</td>
		</tr>
		<tr id="headerDown" style="height:80px; background-color:#F0F0F6">
			<td style="font-weight:bold; font-weight:bold; color:#555555">Critical-to-quality characteristics</td>
		</tr>
		<%for(int i=0; i<ctqs.size(); i++){ 
			CTQs ctq = (CTQs)ctqs.get(i);
			%>
			<tr>
				<td><%=ctq.getCtq() %></td>
				<%for(int j=0; j<functions.size(); j++){ %>
					<td style="width:10%; text-align:center">
						<input id="elem_<%=i+1 %>_<%=j+1 %>" class="qfdField" style="width:100%; text-align:center; border:none" onclick="autoFill('elem_<%=i+1%>_<%=j+1%>')"/>
					</td>
				<%} %>
				<td>
					<%Float imp = Float.valueOf(ctq.getImportance());
					DecimalFormat dFormat = new DecimalFormat("0.00");
					String shownVal = dFormat.format(imp)+"%";%>
					<%=shownVal %>
				</td>
			</tr>
		<%} %>
	</table>
	<div style="width:80%; margin-top:10px; ">
		<button id="calculateQFD" name="calculateQFD" onclick="calculateQFD('<%=ctqs.size()%>','<%=functions.size()%>')" style="width:40%; font-size:small; margin-bottom:10px; font-weight:bold; float:left">Calculate</button>
	</div>
	<div style="clear:both"></div>
	<div id="qfdResults" style="width:80%;">
		<table border="1" cellspacing="0" cellpadding="3" style="width:100%; font-size:small; margin-top: 40px;">
			<tr style="background-color:#F0F0F6">
				<td style="width:60%; font-weight:bold; color:#555555">Product function</td>
				<td style="width:40%; font-weight:bold; color:#555555">Importance</td>
			</tr>
			<%for(int i=0; i<functions.size(); i++){
				ProductFunctions pf = functions.get(i);%>
				<tr>
					<td><%=pf.getFunction() %></td>
					<td id="imp_<%=i %>">&nbsp</td>
				</tr>
			<%} %>
			
		</table>
		<div style="width:100%; margin-top:10px; float:center">
			<button id="finishQfd" name="finishQfd" onclick="finishQfd()" style="width:40%; font-weight: bold; float:left;">Finish analysis</button>
		</div>
		<form name="finishActivityForm" method="post" action="/proddev/controller">
			<input type = "hidden" name = "<%=ControllerConstants.MANAGER_KEY%>" value = "<%=ControllerConstants.ACTIVITY_MANAGER%>"/>
			<input type = "hidden" name = "<%=ControllerConstants.METHOD_KEY%>" value = "<%=ActivityConstants.CHANGE_STATUS%>"/>
			<input id="activityName" name="activityName" type="hidden"/>
		</form>
		<div style="clear:both"></div>
	</div>
	<div style="clear:both"></div>
</div>